# -*- coding: utf-8 -*-
###############################################################################
# file    : views_statistics_admin.py
# author  : wangyubin
# date    : Thu May 15 16:15:39 2014
# 
# brief   : views for 统计信息
# history : init
###############################################################################

# uliweb lib
from uliweb import expose, functions
# python lib
import os
import datetime
# CM-box lib
from db_app.model_define import *
from comm_app.my_log import App_Log
from comm_app import utils

@expose('/admin')
class views_statistics_admin(object):
    """ 统计信息
    """

    @expose('project-statistics')
    def project_statistics(self):
        """ 按项目统计
        """
        # 各个项目bug总数
        projects = T_PROJECT.all()
        project_bugs = []
        for p in projects:
            if p.bugs.count() > 0:
                project_bugs.append({
                    "project_id": p.id,
                    "project_name": p.name,
                    "bug_count": p.bugs.count(),
                })

        project_bugs.sort(reverse=True, cmp=lambda x, y: cmp(x["bug_count"], y["bug_count"]))

        return {
            "project_bugs": project_bugs,
        }


    @expose('project-all-bug-chart')
    def project_all_bug_chart(self):
        """ 所有项目bug信息的图
        """
        projects = T_PROJECT.all()
        records = []
        for p in projects:
            if p.bugs.count() > 0:
                records.append({"project_name": p.name, "bug_count": p.bugs.count()})

        records.sort(reverse=True, cmp=lambda x, y: cmp(x["bug_count"], y["bug_count"]))

        return json({
            "result": "OK",
            "message": "",
            "records": records,
        })


    @expose('project-bug-state-chart')
    def project_bug_state_chart(self):
        """ 各项目中bug状态分布
        """
        p_id = request.POST.get("project_id")
        App_Log.logger.debug(u'p_id: ' + p_id)

        records = []
        for bs in T_BUG_STATE.all():
            records.append({
                "label": bs.name,
                "value": T_BUG.filter(T_BUG.c.bug_state == bs.id).filter(T_BUG.c.project == p_id).count(),
            })
        
        return json({
            "result": "OK",
            "message": "",
            "records": records,
        })


    @expose('project-bug-import-chart')
    def project_bug_import_chart(self):
        """ 各项目中bug重要性分布
        """
        p_id = request.POST.get("project_id")
        App_Log.logger.debug(u'p_id: ' + p_id)

        records = []
        for bi in T_BUG_IMPORTANCE.all():
            records.append({
                "label": bi.name,
                "value": T_BUG.filter(T_BUG.c.bug_importance == bi.id).filter(T_BUG.c.project == p_id).count(),
            })
        
        return json({
            "result": "OK",
            "message": "",
            "records": records,
        })


    @expose('project-bug-change-chart')
    def project_bug_change_chart(self):
        """ 各个项目bug变化趋势
        """
        p_id = request.POST.get("project_id")
        App_Log.logger.debug(u'p_id: ' + p_id)

        today = datetime.date.today()
        records = []
        for day in range(1, 14):
            date = today - datetime.timedelta(days=day)
            date_start_str = utils.datetime2str(date, utils.DATE_FORMAT5)+" 00:00:00.000000"
            date_end_str = utils.datetime2str(date, utils.DATE_FORMAT5)+" 23:59:59.999999"
            date_start_dt = utils.str2datetime(date_start_str, utils.DATE_FORMAT6)
            date_end_dt = utils.str2datetime(date_end_str, utils.DATE_FORMAT6)

            count = T_BUG.filter(T_BUG.c.project == p_id) \
                         .filter(T_BUG.c.create_time > date_start_dt) \
                         .filter(T_BUG.c.create_time <= date_end_dt).count()
            records.append({"date": utils.datetime2str(date, utils.DATE_FORMAT5), "bug_count": count})

        return json({
            "result": "OK",
            "message": "",
            "records": records,
        })


    @expose('user-statistics')
    def user_statistics(self):
        """ 按用户统计
        """
        
        return {}


    @expose('user-create-bugs-chart')
    def user_create_bugs_chart(self):
        """ 各用户创建的 bug
        """
        users = T_USER.all()
        records = []
        for u in users:
            if u.created_bugs.count() > 0:
                records.append({"user_name": u.real_name, "bug_count": u.created_bugs.count()})

        records.sort(reverse=True, cmp=lambda x, y: cmp(x["bug_count"], y["bug_count"]))

        return json({
            "result": "OK",
            "message": "",
            "records": records,
        })

        return json({})




